//
//  HelpEachOtherTableViewCell.m
//  WideHelp
//
//  Created by ENERGY on 2018/8/6.
//  Copyright © 2018年 ENERGY. All rights reserved.
//

#import "HelpEachOtherTableViewCell.h"

@interface HelpEachOtherTableViewCell()
@property (nonatomic, strong)UIImageView * iconImgView;
@property (nonatomic, strong)UILabel * titleLab;
@property (nonatomic, strong)UILabel * contentTopLab;
@property (nonatomic, strong)UILabel * contentBottomLab;
@property (nonatomic, strong)UIButton * functionBtn;
@property (nonatomic, strong)NSString * btnType;
@property (nonatomic, strong)NSString * cellID;//互助ID
@end

@implementation HelpEachOtherTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.contentView.backgroundColor = [UIColor whiteColor];
        __weak typeof(self) weakSelf = self;
        [self.contentView addSubview:self.iconImgView];
        [self.contentView addSubview:self.titleLab];
        [self.contentView addSubview:self.contentTopLab];
        [self.contentView addSubview:self.contentBottomLab];
        [self.contentView addSubview:self.functionBtn];
        int labelBorder = 5/WIDTH_6_SCALE;
        [self.iconImgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.contentView.mas_left).mas_offset(20/WIDTH_6_SCALE);
            make.top.equalTo(weakSelf.contentView.mas_top).mas_offset(16/WIDTH_6_SCALE);
            make.bottom.equalTo(weakSelf.contentView.mas_bottom).mas_offset(-13/WIDTH_6_SCALE);
            make.width.mas_equalTo(80/WIDTH_6_SCALE);
        }];
        [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.iconImgView.mas_right).mas_equalTo(22/WIDTH_6_SCALE);
            make.top.equalTo(weakSelf.contentView.mas_top).mas_equalTo(16/WIDTH_6_SCALE);
            make.height.mas_equalTo(18/WIDTH_6_SCALE);
        }];
        [self.contentTopLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakSelf.titleLab.mas_bottom).mas_equalTo(labelBorder);
            make.left.equalTo(weakSelf.titleLab.mas_left);
        }];
        [self.contentBottomLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakSelf.contentTopLab.mas_bottom).mas_equalTo(labelBorder);
            make.left.height.equalTo(weakSelf.contentTopLab);
        }];
        [self.functionBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(weakSelf.contentView.mas_right).mas_offset(-20/WIDTH_6_SCALE);
            make.width.mas_equalTo(66/WIDTH_6_SCALE);
            make.height.mas_equalTo(30/WIDTH_6_SCALE);
            make.bottom.equalTo(weakSelf.iconImgView.mas_bottom);
        }];
        UIView * lineView = [[UIView alloc] init];
        lineView.backgroundColor = getColor(@"eeeeee");
        [self.contentView addSubview:lineView];
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakSelf.contentView.mas_bottom).mas_offset(-1/WIDTH_6_SCALE);
            make.left.right.equalTo(weakSelf.contentView);
            make.height.mas_equalTo(1/WIDTH_6_SCALE);
        }];
    }return self;
}

#pragma mark - init
-  (UILabel *)titleLab{
    if (!_titleLab) {
        _titleLab = [[UILabel alloc] init];
        _titleLab.font = DEF_FontSize_15;
        _titleLab.textColor = [UIColor blackColor];
        _titleLab.textAlignment = NSTextAlignmentLeft;
        _titleLab.text = @"标题标题标题";
    }return _titleLab;
}

-  (UIImageView *)iconImgView{
    if (!_iconImgView) {
        _iconImgView = [[UIImageView alloc] init];
//        _iconImgView.backgroundColor = [UIColor redColor];
//        _iconImgView.contentMode = UIViewContentModeRedraw;
//        _iconImgView.image = [UIImage imageNamed:@"img_list2"];
    }return _iconImgView;
}

-  (UILabel *)contentTopLab{
    if (!_contentTopLab) {
        _contentTopLab = [[UILabel alloc] init];
        _contentTopLab.font = DEF_FontSize_13;
        _contentTopLab.textColor = [UIColor blackColor];
        _contentTopLab.textAlignment = NSTextAlignmentLeft;
        _contentTopLab.text = @"内容一";
    }return _contentTopLab;
}

-  (UILabel *)contentBottomLab{
    if (!_contentBottomLab) {
        _contentBottomLab = [[UILabel alloc] init];
        _contentBottomLab.font = DEF_FontSize_13;
        _contentBottomLab.textColor = [UIColor blackColor];
        _contentBottomLab.textAlignment = NSTextAlignmentLeft;
        _contentBottomLab.text = @"内容二";
    }return _contentBottomLab;
}

-  (UIButton *)functionBtn{
    if (!_functionBtn) {
        _functionBtn = [[UIButton alloc] init];
        _functionBtn.titleLabel.font = DEF_FontSize_13;
        _functionBtn.layer.cornerRadius = 4;
        _functionBtn.layer.masksToBounds = YES;
    }
    return _functionBtn;
}

- (void)setModel:(NSDictionary *)model{
    _model = model;
    if (!model[@"join_id"] || [model[@"join_id"] isEqualToString:@""] || model[@"join_id"] == nil) {
        [_functionBtn setTitle:@"加入" forState:UIControlStateNormal];
        [_functionBtn setBackgroundColor:getColor(@"f9d047")];
        [_functionBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_functionBtn addTarget:self action:@selector(joininPage:) forControlEvents:UIControlEventTouchUpInside];
    }else{
        [_functionBtn setTitle:@"升级" forState:UIControlStateNormal];
        [_functionBtn setBackgroundColor:getColor(greenBGColor)];
        [_functionBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_functionBtn addTarget:self action:@selector(updatePage:) forControlEvents:UIControlEventTouchUpInside];
    }
    if ([model[@"img"] isEqualToString:@""] || [model[@"img"] length] == 0 || [model[@"img"] isEqual:nil]) {
        self.iconImgView.image = [UIImage imageNamed:@"img_list2"];
    }else{
        [self.imageView sd_setImageWithURL:[NSURL URLWithString:model[@"img"]]];
    }
    self.titleLab.text = model[@"jointitle"];
    self.contentTopLab.text = model[@"helpname"];
    self.contentBottomLab.text = model[@"abstractone"];
    self.cellID = model[@"id"];
}

#pragma mark - action
- (void)joininPage:(UIButton *)sender{
    if (self.joinPageBlock) {
        self.joinPageBlock(self.cellID);
    }
}
- (void)updatePage:(UIButton *)sender{
    if (self.updatePageBlock) {
        self.updatePageBlock(self.cellID);
    }
}
@end
